package com.jdf.jdfapp.activity;

import static android.bluetooth.BluetoothGatt.GATT_SUCCESS;
import android.Manifest;
import androidx.fragment.app.FragmentTransaction;
import android.app.ProgressDialog;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager.widget.ViewPager;

import com.jdf.jdfapp.R;
import com.jdf.jdfapp.utils.BleConstant;
import com.jdf.jdfapp.utils.BleHelper;
import com.jdf.jdfapp.utils.ByteUtils;
import com.jdf.jdfapp.utils.DataTrasportUtil;

import java.util.Arrays;
import java.util.List;
import java.util.Locale;

public class HomeActivity extends FragmentActivity implements TextToSpeech.OnInitListener, ViewPager.OnPageChangeListener {
    private static final String TAG = HomeActivity.class.getSimpleName();
    public static BluetoothDevice bleDevice = null;
    //public  static final String characteristicserviceuuid="00004b50-0000-1000-8000-00805f9b34fb";
    //public  static final String characteristicuuid="0000ffe1-0000-1000-8000-00805f9b34fb";

    public static final String characteristicserviceuuid = "0000fff0-0000-1000-8000-00805f9b34fb";
    public static final String characteristicuuid = "0000fff1-0000-1000-8000-00805f9b34fb";
    /**
     * Gatt
     */
    public static BluetoothGatt bluetoothGatt;
    public static Context context;
    //加载框
    private ProgressDialog progressDialog;
    //语音播报
    public static TextToSpeech textToSpeech; // TTS对象
    private  static StringBuilder tranStr;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);
        context = this;
        //停止测亩
        progressDialog = new ProgressDialog(this);
        progressDialog.show();

        //获取上个页面传递过来的设备
        bleDevice = getIntent().getParcelableExtra("device");
        //连接gatt 设置Gatt回调
        bluetoothGatt = bleDevice.connectGatt(this, false, bluetoothGattCallback);

        textToSpeech = new TextToSpeech(this, this); // 参数Context,TextToSpeech.OnInitListener
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    /**
     * 权限申请回调，可以作进一步处理
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        // 此处为android 6.0以上动态授权的回调，用户自行实现。
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onInit(int status) {
        if (status == TextToSpeech.SUCCESS) {
            int result = textToSpeech.setLanguage(Locale.CHINA);
            if (result == TextToSpeech.LANG_MISSING_DATA
                    || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Toast.makeText(this, "数据丢失或不支持", Toast.LENGTH_SHORT).show();
            }
        } else {
            Log.e("TTS", "Initilization Failed!");
        }
    }

    public static void read(String word) {
        if (textToSpeech != null && !textToSpeech.isSpeaking()) {
            textToSpeech.setPitch(0.0f);// 设置音调，值越大声音越尖（女生），值越小则变成男声,1.0是常规
            textToSpeech.speak(word,
                    TextToSpeech.QUEUE_FLUSH, null);
        }
    }

    //蓝牙回调函数
    private final BluetoothGattCallback bluetoothGattCallback = new BluetoothGattCallback() {
        /**
         * 连接状态改变回调
         * @param gatt     gatt
         * @param status   gatt连接状态
         * @param newState 新状态
         */
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
            Log.d(TAG, Thread.currentThread().getName());
            if (status == BluetoothGatt.GATT_SUCCESS) {
                progressDialog.dismiss();
                switch (newState) {
                    case BluetoothProfile.STATE_CONNECTED://连接成功
                        Log.d(TAG, "连接成功");
                        //获取MtuSize
                        gatt.requestMtu(512);
                        break;
                    case BluetoothProfile.STATE_DISCONNECTED://断开连接
                        Log.e(TAG, "断开连接");
                        break;
                    default:
                        break;
                }
            } else {
                Intent intent = new Intent(context, BlueToolthActivity.class);
                startActivity(intent);
                Toast.makeText(context, "蓝牙断开了", Toast.LENGTH_SHORT).show();
                Log.e(TAG, "onConnectionStateChange: " + status);
            }
        }

        /**
         * 物理层改变回调
         * @param gatt   gatt
         * @param txPhy  发送速率  1M 2M
         * @param rxPhy  接收速率  1M 2M
         * @param status 更新操作的状态
         */
        @Override
        public void onPhyUpdate(BluetoothGatt gatt, int txPhy, int rxPhy, int status) {
            Log.d(TAG, "onPhyUpdate: txPhy: " + txPhy + " rxPhy: " + rxPhy);
        }

        /**
         * 读取物理层回调
         * @param gatt   gatt
         * @param txPhy  发送速率  1M 2M
         * @param rxPhy  接收速率  1M 2M
         * @param status 更新操作的状态
         */
        @Override
        public void onPhyRead(BluetoothGatt gatt, int txPhy, int rxPhy, int status) {
            Log.d(TAG, "onPhyRead: txPhy：" + txPhy + " rxPhy：" + rxPhy);
            if (txPhy == BluetoothDevice.PHY_LE_1M && rxPhy == BluetoothDevice.PHY_LE_1M) {
                //获取2M的发送和接收速率
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    gatt.setPreferredPhy(BluetoothDevice.PHY_LE_2M, BluetoothDevice.PHY_LE_2M,
                            BluetoothDevice.PHY_OPTION_NO_PREFERRED);
                }
            }
        }

        /**
         * 发现服务回调
         * @param gatt   gatt
         * @param status gatt状态
         */
        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            Log.d(TAG, "onServicesDiscovered");
            boolean notifyOpen = BleHelper.enableIndicateNotification(gatt);

            if (!notifyOpen) {
                Log.e(TAG, "开启通知属性异常");
                gatt.disconnect();
            }
        }

        /**
         * 特性读取回调
         * @param gatt           gatt
         * @param characteristic 特性
         * @param status         gatt状态
         */
        @Override
        public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
            Log.d(TAG, "onCharacteristicRead: characteristic: " + characteristic.getUuid().toString());
        }

        /**
         * 特性写入回调
         * @param gatt           gatt
         * @param characteristic 特性
         * @param status         gatt状态
         */
        @Override
        public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
            String command = ByteUtils.bytesToHexString(characteristic.getValue());
            if (status == BluetoothGatt.GATT_SUCCESS) {
                Log.d(TAG, "onCharacteristicWrite: 写入成功：" + command);
            } else {
                Log.d(TAG, "onCharacteristicWrite: 写入失败：" + command);
            }
        }

        /**
         * 特性改变回调
         * @param gatt
         * @param characteristic 特性
         */
        @Override
        public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
            String strString = ByteUtils.bytesToHexString(characteristic.getValue());
            //String strString ="6801200000aa0000000000000000000000000000000000000000000000000000000000e316";
            System.out.println("收到数据:" + strString);
            String bccdata = strString.substring(0, strString.length() - 4);
            if(strString.startsWith("68")&&strString.endsWith("16")){
                if(yiHuo(bccdata).equalsIgnoreCase(strString.substring(strString.length() - 4,strString.length()-2))){ //bcc校验通过
                    String data = strString.substring(6, strString.length() - 4);
                    String substring = strString.substring(4, 6);
                    int length = Integer.parseInt(substring,16);
                    StringBuilder binary = new StringBuilder("");
                    if(2*length==data.length()){
                        //68 0000000000000000 0000000000000000 0000000000000000 0000000000000000 16
                        for(int i=0;i<data.length();i+=2){
                            StringBuilder s = new StringBuilder(Integer.toBinaryString(Integer.parseInt(data.substring(i, i + 2),16))).reverse();
                            while (s.length()<8){
                                s.append("0");
                            }
                            binary.append(s);
                            //System.out.println(s);
                            /*for(int j=0;j<8;j++){
                                reslovedata(i,j,s.substring(j,j+1));
                            }*/
                        }
                        tranStr=new StringBuilder("");
                        tranStr.append("原始数据："+"\n    "+data.substring(0,8)+"    "+data.substring(8,16)+"\n    "+data.substring(16,24)+"    "+data.substring(24,32)+"\n\n");
                        reslovebinaydata(binary);
                        TextView dataview = findViewById(R.id.data);
                        dataview.setText(tranStr);
                    }else {
                        System.out.println("长度不对");
                    }
                }else {
                    //未经过bcc校验
                    System.out.println("未经过bcc校验");
                }
            }else {
                System.out.println("帧格式不对");
            }
        }
        /**
         * 描述符获取回调
         * @param gatt       gatt
         * @param descriptor 描述符
         * @param status     gatt状态
         */
        @Override
        public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
            Log.d(TAG, "onDescriptorRead: descriptor: " + descriptor.getUuid().toString());
        }

        /**
         * 描述符写入回调
         * @param gatt       gatt
         * @param descriptor 描述符
         * @param status     gatt状态
         */
        @Override
        public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
            if (BleConstant.DESCRIPTOR_UUID.equalsIgnoreCase(descriptor.getUuid().toString())) {
                if (status == GATT_SUCCESS) {
                    Log.d(TAG, "onDescriptorWrite: 通知开启成功");
                    //获取phy
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                        gatt.readPhy();
                    }
                    //读取描述符
                    gatt.readDescriptor(descriptor);
                    //读取RSSI
                    gatt.readRemoteRssi();
                } else {
                    Log.d(TAG, "onDescriptorWrite: 通知开启失败");
                }
            }
        }

        /**
         * 可靠写入完成回调
         * @param gatt   gatt
         * @param status gatt状态
         */
        @Override
        public void onReliableWriteCompleted(BluetoothGatt gatt, int status) {
            Log.d(TAG, "onReliableWriteCompleted: 可靠写入");
        }

        /**
         * 读取远程设备的信号强度回调
         * @param gatt   gatt
         * @param rssi   信号强度
         * @param status gatt状态
         */
        @Override
        public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
            Log.d(TAG, "onReadRemoteRssi: rssi: " + rssi);
        }

        /**
         * Mtu改变回调
         * @param gatt   gatt
         * @param mtu    new MTU size
         * @param status gatt状态
         */
        @Override
        public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
            Log.d(TAG, "onMtuChanged：mtu： " + mtu);
            //发现服务
            gatt.discoverServices();
        }
    };

    /**
     * BCC数据校验 异或处理
     */
    public static String yiHuo(String content) {
        byte[] bytes = DataTrasportUtil.HexString2Bytes(content);
        byte b = calculateChecksum(bytes);
        return DataTrasportUtil.byteToHex(b);
    }

    /**
     * 异或校验
     */
    private static byte calculateChecksum(byte[] data) {
        byte checksum = 0;
        for (byte b : data) {
            checksum ^= b;
        }
        return checksum;
    }

    /**
     * 解析每一位数据
     *
     */
    private void reslovedata(int i,int j,String s){
        switch (i){
            case 0:
                switch (j){
                    case 0:
                        if(s.equals(0)){
                            //割台上升
                        }
                        break;
                    case 1:
                        if(s.equals(0)){
                            //割台下降
                        }
                        break;
                    case 2:
                        if(s.equals(0)){
                            //拨禾轮上升
                        }
                        break;
                    case 3:
                        if(s.equals(0)){
                            //拨禾轮下降
                        }
                        break;
                    case 4:
                        if(s.equals(0)){
                            //粮箱上升
                        }
                        break;
                    case 5:
                        if(s.equals(0)){
                            //粮箱下降
                        }
                        break;
                    case 6:
                        if(s.equals(0)){
                            //主离合结合
                        }
                        break;
                    case 7:
                        if(s.equals(0)){
                            //主离合分离
                        }
                        break;
                }
                break;
            case 2:
                switch (j){
                    case 0:
                        if(s.equals(0)){
                            //籽粒卸粮
                        }
                        break;

                    case 2:
                        if(s.equals(0)){
                            //割台复位
                        }
                        break;
                    case 4:
                        if(s.equals(0)){
                            //刹车开关
                        }
                        break;

                    case 7:
                        if(s.equals(0)){
                            //总线手柄异常
                        }
                        break;
                }
                break;
            case 4:
                switch (j){
                    case 0:
                        if(s.equals(0)){
                            //割台上升电磁阀
                        }
                        break;
                    case 1:
                        if(s.equals(0)){
                            //割台下降电磁阀
                        }
                        break;
                    case 2:
                        if(s.equals(0)){
                            //拨禾轮上升电磁阀
                        }
                        break;
                    case 3:
                        if(s.equals(0)){
                            //拨禾轮下降电磁阀
                        }
                        break;
                    case 4:
                        if(s.equals(0)){
                            //粮箱上升电磁阀
                        }
                        break;
                    case 5:
                        if(s.equals(0)){
                            //粮箱下降电磁阀
                        }
                        break;
                    case 6:
                        if(s.equals(0)){
                            //主离合结合电磁阀
                        }
                        break;
                    case 7:
                        if(s.equals(0)){
                            //主离合分离电磁阀
                        }
                        break;
                }
                break;
            case 6:
                switch (j){
                    case 0:
                        if(s.equals(0)){
                            //籽粒卸粮电磁阀
                        }
                        break;

                    case 2:
                        if(s.equals(0)){
                            //刹车电磁阀
                        }
                        break;
                    case 3:
                        if(s.equals(0)){
                            //总阀
                        }
                        break;
                }
                break;
            case 8:
                switch (j){
                    case 0:
                        if(s.equals(0)){
                            //割台上升阀开路
                        }
                        break;
                    case 1:
                        if(s.equals(0)){
                            //割台上升阀短路
                        }
                        break;
                    case 2:
                        if(s.equals(0)){
                            //割台下降阀开路
                        }
                        break;
                    case 3:
                        if(s.equals(0)){
                            //割台下降阀短路
                        }
                        break;
                    case 4:
                        if(s.equals(0)){
                            //拨禾轮上升阀开路
                        }
                        break;
                    case 5:
                        if(s.equals(0)){
                            //拨禾轮上升阀短路
                        }
                        break;
                    case 6:
                        if(s.equals(0)){
                            //拨禾轮下降阀开路
                        }
                        break;
                    case 7:
                        if(s.equals(0)){
                            //拨禾轮下降阀短路
                        }
                        break;
                }
                break;
            case 10:
                switch (j){
                    case 0:
                        if(s.equals(0)){
                            //粮箱上升阀开路
                        }
                        break;
                    case 1:
                        if(s.equals(0)){
                            //粮箱上升阀短路
                        }
                        break;
                    case 2:
                        if(s.equals(0)){
                            //粮箱下降阀开路
                        }
                        break;
                    case 3:
                        if(s.equals(0)){
                            //粮箱下降阀短路
                        }
                        break;
                    case 4:
                        if(s.equals(0)){
                            //主离合结合阀开路
                        }
                        break;
                    case 5:
                        if(s.equals(0)){
                            //主离合结合阀短路
                        }
                        break;
                    case 6:
                        if(s.equals(0)){
                            //主离合分离阀开路
                        }
                        break;
                    case 7:
                        if(s.equals(0)){
                            //主离合分离阀短路
                        }
                        break;
                }
                break;
            case 12:
                switch (j){
                    case 0:
                        if(s.equals(0)){
                            //籽粒卸粮阀开路
                        }
                        break;
                    case 1:
                        if(s.equals(0)){
                            //籽粒卸粮阀短路
                        }
                        break;

                    case 4:
                        if(s.equals(0)){
                            //刹车阀开路
                        }
                        break;
                    case 5:
                        if(s.equals(0)){
                            //刹车阀短路
                        }
                        break;
                    case 6:
                        if(s.equals(0)){
                            //总阀开路
                        }
                        break;
                    case 7:
                        if(s.equals(0)){
                            //总阀短路
                        }
                        break;
                }
                break;
            case 14:
                switch (j){
                    case 0:
                        if(s.equals(0)){
                            //行走运行状态
                        }
                        break;
                    case 1:
                        if(s.equals(0)){
                            //发动机运行状态
                        }
                        break;
                    case 2:
                        if(s.equals(0)){
                            //PTO检测状态
                        }
                        break;
                    case 3:
                        if(s.equals(0)){
                            //PTO输出状态
                        }
                        break;
                    case 4:
                        if(s.equals(0)){
                            //卸粮筒未收回报警
                        }
                        break;
                    case 5:
                        if(s.equals(0)){
                            //手动/自动模式
                        }
                        break;
                    case 6:
                        if(s.equals(0)){
                            //APP未注册
                        }
                        break;
                    case 7:
                        if(s.equals(0)){
                            //输出开短路故障信息
                        }
                        break;
                }
                break;


            case 26:
                switch (j){
                    case 0:
                        if(s.equals(0)){
                            //北斗绑定状态
                        }
                        break;
                    case 1:
                        if(s.equals(0)){
                            //北斗锁车状态
                        }
                        break;
                }
                break;
            case 28:
                switch (j){
                    case 0:
                        if(s.equals(0)){
                            //新小纵机滚筒无级变速加速状态
                        }
                        break;
                    case 1:
                        if(s.equals(0)){
                            //新小纵机滚筒无级变速减速状态
                        }
                        break;
                }
                break;





            case 32:
                break;
            case 34:
                break;
            case 36:
                break;
            case 38:
                break;



            case 48:
                break;
            case 50:
                break;
            case 52:
                break;
            case 54:
                break;
            case 56:
                break;
            case 58:
                break;
            case 60:
                break;
            case 62:
                break;
        }
    }

    private void reslovebinaydata(StringBuilder binaydata){
        char[] chars = binaydata.toString().toCharArray();
        tranStr.append("解析数据：\n    ");
        for(int i=0; i<chars.length; i++){
            switch (i){
                case 0://割台上升按钮
                    showtext(chars,i,"割台上升");
                    break;
                case 1://割台下降按钮
                    showtext(chars,i,"割台下降");
                    break;
                case 2://还田机/拨禾轮上升按钮
                    showtext(chars,i,"还田机/拨禾轮上升");
                    break;
                case 3://还田机/拨禾轮下降按钮
                    showtext(chars,i,"还田机/拨禾轮下降");
                    break;
                case 4://粮箱上升按钮
                    showtext(chars,i,"粮箱上升");
                    break;
                case 5://粮箱下降按钮
                    showtext(chars,i,"粮箱下降");
                    break;
                case 6://主离合结合按钮
                    showtext(chars,i,"主离合结合");
                    break;
                case 7://主离合分离按钮
                    showtext(chars,i,"主离合分离");
                    break;
                case 8://籽粒卸粮按钮
                    showtext(chars,i,"籽粒卸粮");
                    break;


                case 10://割台复位按钮
                    showtext(chars,i,"割台复位");
                    break;

                case 12://刹车开关信号
                    showtext(chars,i,"刹车开关信号");
                    break;

                case 15://总线手柄异常
                    showtext(chars,i,"总线手柄");
                    break;
                case 16://割台上升电磁阀
                    showtext(chars,i,"割台上升电磁阀");
                    break;
                case 17://割台下降电磁阀
                    showtext(chars,i,"割台下降电磁阀");
                    break;
                case 18://还田机/拨禾轮上升电磁阀
                    showtext(chars,i,"还田机/拨禾轮上升电磁阀");
                    break;
                case 19://还田机/拨禾轮下降电磁阀
                    showtext(chars,i,"还田机/拨禾轮下降电磁阀");
                    break;

                case 20://粮箱上升电磁阀
                    showtext(chars,i,"粮箱上升电磁阀");
                    break;
                case 21://粮箱下降电磁阀
                    showtext(chars,i,"粮箱下降电磁阀");
                    break;
                case 22://主离合结合电磁阀
                    showtext(chars,i,"主离合结合电磁阀");
                    break;
                case 23://主离合分离电磁阀
                    showtext(chars,i,"主离合分离电磁阀");
                    break;
                case 24://籽粒卸粮电磁阀
                    showtext(chars,i,"籽粒卸粮电磁阀");
                    break;

                case 26://刹车电磁阀
                    showtext(chars,i,"刹车电磁阀");
                    break;
                case 27://总阀
                    showtext(chars,i,"总阀");
                    break;

                case 32://割台上升阀开路
                    showtext(chars,i,"割台上升阀开路");
                    break;
                case 33://割台上升阀短路
                    showtext(chars,i,"割台上升阀短路");
                    break;
                case 34://割台下降阀开路
                    showtext(chars,i,"割台下降阀开路");
                    break;
                case 35://割台下降阀短路
                    showtext(chars,i,"割台下降阀短路");
                    break;
                case 36://还田机/拨禾轮上升阀开路
                    showtext(chars,i,"还田机/拨禾轮上升阀开路");
                    break;
                case 37://还田机/拨禾轮上升阀短路
                    showtext(chars,i,"还田机/拨禾轮上升阀短路");
                    break;
                case 38://还田机/拨禾轮下降阀开路
                    showtext(chars,i,"还田机/拨禾轮下降阀开路");
                    break;
                case 39://还田机/拨禾轮下降阀短路
                    showtext(chars,i,"还田机/拨禾轮下降阀短路");
                    break;

                case 40://粮箱上升阀开路
                    showtext(chars,i,"粮箱上升阀开路");
                    break;
                case 41://粮箱上升阀短路
                    showtext(chars,i,"粮箱上升阀短路");
                    break;
                case 42://粮箱下降阀开路
                    showtext(chars,i,"粮箱下降阀开路");
                    break;
                case 43://粮箱下降阀短路
                    showtext(chars,i,"粮箱下降阀短路");
                    break;
                case 44://主离合结合阀开路
                    showtext(chars,i,"主离合结合阀开路");
                    break;
                case 45://主离合结合阀短路
                    showtext(chars,i,"主离合结合阀短路");
                    break;
                case 46://主离合分离阀开路
                    showtext(chars,i,"主离合分离阀开路");
                    break;
                case 47://主离合分离阀短路
                    showtext(chars,i,"主离合分离阀短路");
                    break;
                case 48://籽粒卸粮阀开路
                    showtext(chars,i,"籽粒卸粮阀开路");
                    break;
                case 49://籽粒卸粮阀短路
                    showtext(chars,i,"籽粒卸粮阀短路");
                    break;


                case 52://刹车阀开路
                    showtext(chars,i,"刹车阀开路");
                    break;
                case 53://刹车阀短路
                    showtext(chars,i,"刹车阀短路");
                    break;
                case 54://总阀开路
                    showtext(chars,i,"总阀开路");
                    break;
                case 55://总阀短路
                    showtext(chars,i,"总阀短路");
                    break;
                case 56://行走运行状态
                    showtext(chars,i,"行走运行状态");
                    break;
                case 57://发动机运行状态
                    showtext(chars,i,"发动机运行状态");
                    break;
                case 58://PTO检测状态
                    showtext(chars,i,"PTO检测状态");
                    break;
                case 59://PTO输出状态
                    showtext(chars,i,"PTO输出状态");
                    break;

                case 60://卸粮筒未收回报警
                    showtext(chars,i,"卸粮筒未收回报警");
                    break;
                case 61://手动/自动模式
                    showtext(chars,i,"手动/自动模式");
                    break;
                case 62://APP未注册
                    showtext(chars,i,"APP未注册");
                    break;
                case 63://输出开短路故障信息
                    showtext(chars,i,"输出开短路故障信息");
                    break;

                case 104://北斗绑定状态
                    showtext(chars,i,"北斗绑定状态");
                    break;
                case 105://北斗锁车状态
                    showtext(chars,i,"北斗锁车状态");
                    break;

                case 112://新小纵机滚筒无级变速加速状态
                    showtext(chars,i,"新小纵机滚筒无级变速加速状态");
                    break;
                case 113://新小纵机滚筒无级变速减速状态
                    showtext(chars,i,"新小纵机滚筒无级变速减速状态");
                    break;


                case 128://128-135 高度设定电位计（低8位） 136-143 高度设定电位计（高8位）
                    StringBuilder heightlow8 =new StringBuilder(""+chars[i]+chars[i+1]+chars[i+2]+chars[i+3]+chars[i+4]+chars[i+5]+chars[i+6]+chars[i+7]);
                    StringBuilder reverseheightlow8 = heightlow8.reverse();
                    String heightlowhex = Integer.toHexString(Integer.parseInt(String.valueOf(reverseheightlow8), 2));

                    StringBuilder heighthigh8= new StringBuilder(""+chars[i+8]+chars[i+9]+chars[i+10]+chars[i+11]+chars[i+12]+chars[i+13]+chars[i+14]+chars[i+15]);
                    StringBuilder reverseheighthigh8 = heighthigh8.reverse();
                    String heighthighhex = Integer.toHexString(Integer.parseInt(String.valueOf(reverseheighthigh8), 2));
                    String heighthex=heighthighhex+heightlowhex;
                    showtext(chars,i,"高度设定电位计："+Integer.parseInt(heighthex,16));
                    break;

                case 144://144-151 割台角度传感器(低8位)  152-159 割台角度传感器(高8位)
                    StringBuilder getailow8 =new StringBuilder(""+chars[i]+chars[i+1]+chars[i+2]+chars[i+3]+chars[i+4]+chars[i+5]+chars[i+6]+chars[i+7]);
                    StringBuilder reversegetailow8 = getailow8.reverse();
                    String getailowhex = Integer.toHexString(Integer.parseInt(String.valueOf(reversegetailow8), 2));

                    StringBuilder getaihigh8= new StringBuilder(""+chars[i+8]+chars[i+9]+chars[i+10]+chars[i+11]+chars[i+12]+chars[i+13]+chars[i+14]+chars[i+15]);
                    StringBuilder reversegetaihigh8 = getaihigh8.reverse();
                    String getaihighhex = Integer.toHexString(Integer.parseInt(String.valueOf(reversegetaihigh8), 2));
                    String getaihex=getaihighhex+getailowhex;
                    showtext(chars,i,"割台角度传感器："+Integer.parseInt(getaihex,16));
                    break;

                case 192://电控手柄信号值（0-1000） 192-201
                    StringBuilder powerhandlelow8 =new StringBuilder(""+chars[i]+chars[i+1]+chars[i+2]+chars[i+3]+chars[i+4]+chars[i+5]+chars[i+6]+chars[i+7]);
                    StringBuilder reversepowerhandlelow8 = powerhandlelow8.reverse();
                    String powerhandlelowhex = Integer.toHexString(Integer.parseInt(String.valueOf(reversepowerhandlelow8), 2));

                    StringBuilder powerhandlehigh8= new StringBuilder(""+chars[i+8]+chars[i+9]+chars[i+10]+chars[i+11]+chars[i+12]+chars[i+13]+chars[i+14]+chars[i+15]);
                    StringBuilder reversepowerhandlehigh8 = powerhandlehigh8.reverse();
                    String powerhandlehighhex = Integer.toHexString(Integer.parseInt(String.valueOf(reversepowerhandlehigh8), 2));
                    String powerhandlehex=powerhandlelowhex+powerhandlehighhex;

                    int powerhandlenum = Integer.parseInt(powerhandlehex, 16);
                    int num = powerhandlenum >> 6;
                    showtext(chars,i,"电控手柄信号值（0-1000）："+num);
                    break;

                case 202://前进标识
                    showtext(chars,i,"前进标识");
                    break;
                case 203://后退标识
                    showtext(chars,i,"后退标识");
                    break;
                case 204://1档
                    showtext(chars,i,"1档");
                    break;
                case 205://2档
                    showtext(chars,i,"2档");
                    break;
                case 206://3档
                    showtext(chars,i,"3档");
                    break;

                case 208://208-215  前进电磁阀开度
                    StringBuilder front =new StringBuilder(""+chars[i]+chars[i+1]+chars[i+2]+chars[i+3]+chars[i+4]+chars[i+5]+chars[i+6]+chars[i+7]);
                    StringBuilder reversfront = front.reverse();
                    int frontnum = Integer.parseInt(String.valueOf(reversfront), 2);
                    showtext(chars,i,"前进电磁阀开度："+frontnum);
                    break;

                case 216://216-223 后退电磁阀开度
                    StringBuilder after =new StringBuilder(""+chars[i]+chars[i+1]+chars[i+2]+chars[i+3]+chars[i+4]+chars[i+5]+chars[i+6]+chars[i+7]);
                    StringBuilder reversafter = after.reverse();
                    int afternum = Integer.parseInt(String.valueOf(reversafter), 2);
                    showtext(chars,i,"后退电磁阀开度："+afternum);
                    break;
                case 224://四驱/两驱
                    showtext(chars,i,"四驱/两驱");
                    break;
                case 225://重载/轻载
                    showtext(chars,i,"重载/轻载");
                    break;
                case 226://转场状态
                    showtext(chars,i,"转场状态");
                    break;

                case 232://前进比例阀开路
                    showtext(chars,i,"前进比例阀开路");
                    break;
                case 233://前进比例阀短路
                    showtext(chars,i,"前进比例阀短路");
                    break;
                case 234://后退比例阀开路
                    showtext(chars,i,"后退比例阀开路");
                    break;
                case 235://后退比例阀短路
                    showtext(chars,i,"后退比例阀短路");
                    break;

                case 248://248-256 行走部分故障信息：1=手柄上电时不处于中位 2=手柄断线（故障）（后续根据需求进行添加）
                    break;
            }
        }

    }

    private void showtext(char[] chars,int index,String word){
        if(chars[index]=='1'){
            tranStr.append(word+"\n    ");
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {
        super.onPointerCaptureChanged(hasCapture);
    }
}
